package ai.mspbots.poc.controller.v2;

import ai.mspbots.poc.db.entity.SearchHistoryLog;
import ai.mspbots.poc.db.service.SearchHistoryLogService;
import ai.mspbots.poc.dto.SearchHistoryLogDTO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/v2/es")
public class HistoryController {

    @Autowired
    private SearchHistoryLogService searchHistoryLogService;

    @GetMapping("/history/list")
    public List<SearchHistoryLog> getSearchHistoryLogList(String keyword) {
        return searchHistoryLogService.list(Wrappers.lambdaQuery(SearchHistoryLog.class)
                // .like(SearchHistoryLog::getKeyword, keyword)
                .orderByDesc(SearchHistoryLog::getCreateTime)
                .last("NULLS LAST LIMIT 1000"))
                ;
    }

    @PostMapping("/history/create")
    public void createSearchHistoryLog(@RequestBody SearchHistoryLogDTO searchHistoryLogDTO) {
        searchHistoryLogService.save(new SearchHistoryLog(searchHistoryLogDTO));
    }
}
